﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinForm {
    public partial class FormContactList : Form
    {
        BLL.Contact contact = new BLL.Contact();
        public FormContactList()
        {
            InitializeComponent();
        }

        private void FormContactList_Load(object sender, EventArgs e)
        {
            Fill();
        }

        public void Fill()
        {
            string strWhere = "";
            if(cboCondition.Text == "姓名") {
                strWhere = " name like %" + txtSearch.Text.Trim() + "%";
            }
            if (cboCondition.Text == "手机")
                strWhere = " phone like %" + txtSearch.Text.Trim() + "%";
            dgvContactList.DataSource = contact.GetList(strWhere);
        }

        private void dgvContactList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void FormContactList_Resize(object sender, EventArgs e)
        {
            

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Fill();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormContactAdd f = new FormContactAdd();
            f.ShowDialog();
            Fill();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                id = (int)dgvContactList.CurrentRow.Cells[0].Value;
            }catch(Exception ex)
            {
                MessageBox.Show("请选择有效的记录！");
                return;
            }
            DialogResult dr = MessageBox.Show("是否确定删除选择的联系人？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                
                if (contact.Delete(id))
                {
                    MessageBox.Show("成功删除联系人！");
                }
                else
                {
                    MessageBox.Show("删除联系人失败！");
                }
            }
            Fill();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvContactList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = 0;
            try
            {
                id = (int)dgvContactList.CurrentRow.Cells[0].Value;
            }catch(Exception ex)
            {
                MessageBox.Show("请选择有效的记录！");
                return;
            }
            FormContactDetail f = new FormContactDetail(id);
            f.ShowDialog();
            Fill();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                id = (int)dgvContactList.CurrentRow.Cells[0].Value;
            }
            catch (Exception ex)
            {
                MessageBox.Show("请选择有效的记录！");
                return;
            }
            FormContactDetail f = new FormContactDetail(id);
            f.ShowDialog();
            Fill();
        }
    }
}
